package es.cazadoresmadridejos.beans.controller.reason;

import javax.servlet.http.HttpServletRequest;

import org.displaytag.pagination.PaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.reason.ReasonManager;
import es.cazadoresmadridejos.objs.web.Page;


@SessionAttributes({"reasonCriteria"})
@Controller
public class ReasonListController {

	@Autowired private ReasonManager reasonManager;

	@RequestMapping(value="/reasonList*.html")
	public ModelAndView list(
			HttpServletRequest request,
			Boolean isList
			) throws Throwable {
		
		ModelAndView mv;
		if(Boolean.TRUE.equals(isList)){
			mv = new ModelAndView("reasonList");
		}else{
			mv = new ModelAndView();
		}

		Page page = new Page(request);
		
		PaginatedList list = reasonManager.findAllPaginated(page);

		mv.addObject("list", list);
		return mv;
	}
	
	
	@RequestMapping(value="/reasonDelete.html")
	public ModelAndView delete(
			@RequestParam(value="id")Long id,
			HttpServletRequest request
			) throws Throwable {

		reasonManager.delete(id);
		
		return list(request,true);
	}
	
	
}
